﻿function formatDisplay(val) {
    return commonDisplay(val, "正常", "已禁用");
}
function format_disable(val) {
    return commonDisplay(val, "正常", "已禁用");
}
function format_delete(val) {
    return commonDisplay(val, "正常", "已删除");
}

function format_yesno(val) {
    return commonDisplay(val, "否", "是");
}
function format_pass(val) {
    if (val) {
        return "已审核";
    } else {
        return "<span style='color:red'>未审核</span>";
    }
}

function commonDisplay(val, success, error) {
    if (!val) {
        return success;
    } else {
        return "<span style='color:red'>" + error + "</span>";
    }
}
function formatIsIndex(val) {
    if (!val) {
        return "否";
    } else {
        return "<span style='color:red'>是</span>";
    }
}
function fixWidth(percent) {
    return document.body.clientWidth * percent; //这里你可以自己做调整  
}

function newPage(title, url, width, height) {
    if (arguments.length == 2) {
        newPageMax(title, url);
    } else {
        if ($("iframe").length == 0)
            $('#openWindow').append("<iframe marginwidth='0' marginheight='0' frameborder='0' scrolling='auto' width='100%' height='95%' src='" + url + "'></iframe>");
        $("#openWindow").window({
            width: width || 700,
            height: height || 500,
            title: title || "",
            modal: true,
            onClose: function () {
                $("#dg").datagrid("reload");
                $("#openWindow").html("");
            }
        });
    }
}
function newPageMax(title, url) {
    if ($("iframe").length == 0)
        $('#openWindow').append("<iframe marginwidth='0' marginheight='0' frameborder='0' scrolling='auto' width='100%' height='95%' src='" + url + "'></iframe>");
    $("#openWindow").window({
        title: title || "",
        modal: true,
        maximized: true,
        onClose: function () {
            $("#dg").datagrid("reload");
            $("#openWindow").html("");
        }
    });
}
function submitForm() {
    $('.validate').form({
        success: function (data) {
            var json = eval('(' + data + ')');
            if (json.State == "1") {
                $.messager.alert('提示', json.Message, "success", function () {
                    closeWindow();
                });
            } else {
                $.messager.alert("错误", json.Message, "error");
            }
        }
    });
    $(".validate").submit();
}

function closeWindow(id) {
    id = id || "openWindow";
    parent.$("#" + id).window("close");
    parent.$("#" + id).html("");
}
function getId() {
    var row = $("#dg").datagrid('getSelected');
    if (row) {
        return row.Id;
    } else {
        $.messager.alert("错误", "请选择一条记录！", "info");
    }
}
///通用删除
function Delete(url) {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        $.messager.confirm('Confirm', '确定要删除?', function (r) {
            if (r) {
                $.post(url, { id: row.Id }, function (data) {
                    if (data.State == "1") {
                        $.messager.alert('成功', data.Message, "success", function () {
                            $('#dg').datagrid('reload');
                        });
                    } else {
                        $.messager.alert('错误', data.Message);
                    }
                }, 'json');
            }
        });
    } else {
        $.messager.alert("错误", "请选择一条记录进行删除！", "info");
    }
}

function edit(title, url, width, height) {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        if ($("iframe").length == 0) {
            url += "?id=" + row.Id;
            $('#openWindow').append("<iframe marginwidth='0' marginheight='0' frameborder='0' scrolling='auto' width='100%' height='95%' src='" + url + "'></iframe>");
        }
        $("#openWindow").window({
            width: width || 700,
            height: height || 500,
            title: title || "",
            modal: true,
            onClose: function () {
                $("#dg").datagrid("reload");
                $("#openWindow").html("");
            }
        });
    } else {
        $.messager.alert("错误", "请选择一条记录进行修改！", "info");
    }
}
function editMax(title, url) {
    var row = $('#dg').datagrid('getSelected');
    if (row) {
        if ($("iframe").length == 0) {
            url += "?id=" + row.Id;
            $('#openWindow').append("<iframe marginwidth='0' marginheight='0' frameborder='0' scrolling='auto' width='100%' height='95%' src='" + url + "'></iframe>");
        }
        $("#openWindow").window({
            title: title || "",
            modal: true,
            maximized: true,
            onClose: function () {
                $("#dg").datagrid("reload");
                $("#openWindow").html("");
            }
        });
    } else {
        $.messager.alert("错误", "请选择一条记录进行修改！", "info");
    }
}

function fixWidthTable(percent) {
    return getWidth(1) * percent;
}

function getWidth(percent) {
    return $(window).width() * percent;
}

$(function () {
    $(window).resize(function () {
        $("#dg").datagrid("resize", { width: getWidth(1) });
    });
});

function OperationId(message, url) {
    $.messager.confirm('Confirm', message, function (r) {
        if (r) {
            $.post(url, { id: getId() }, function (data) {
                if (data.State == "1") {
                    $.messager.alert('成功', data.Message, "success", function () {
                        $('#dg').datagrid('reload');
                    });
                } else {
                    $.messager.alert('错误', data.Message);
                }
            }, "json");
        }
    });
}



function SingleUpload(repath, uppath, iswater, callBack) {
    var submitUrl = "/Upload/SingleUpload?ReFilePath=" + repath + "&UpFilePath=" + uppath;
    //判断是否打水印
    if (arguments.length == 3) {
        submitUrl = "/Upload/SingleUpload?ReFilePath=" + repath + "&UpFilePath=" + uppath + "&IsWater=" + iswater;
    }
    //开始提交
    $("#form1").ajaxSubmit({
        beforeSubmit: function (formData, jqForm, options) {
            //隐藏上传按钮
            $("#" + repath).nextAll(".files").eq(0).hide();
            //显示LOADING图片
            $("#" + repath).nextAll(".uploading").eq(0).show();
        },
        success: function (data, textStatus) {
            if (data.State == 1) {
                $("#" + repath).val(data.Message);
                if (arguments.length == 4) {
                    callBack();
                }
            } else {
                $.messager.alert('错误', data.Message);
            }
            $("#" + repath).nextAll(".files").eq(0).show();
            $("#" + repath).nextAll(".uploading").eq(0).hide();

        },
        error: function (data, status, e) {
            alert("上传失败，错误信息：" + e);
            $("#" + repath).nextAll(".files").eq(0).show();
            $("#" + repath).nextAll(".uploading").eq(0).hide();
        },
        url: submitUrl + "&path=" + $("#path").val(),
        type: "post",
        dataType: "json",
        timeout: 600000
    });
    return false;
};


//将表单数据转为json
function form2Json(id) {
    var arr = $("#" + id).serializeArray();
    var jsonStr = "";

    jsonStr += '{';
    for (var i = 0; i < arr.length; i++) {
        jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",'
    }
    jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
    jsonStr += '}';

    var json = JSON.parse(jsonStr);
    return json;
}
$(function () {
    $("#submit_search").click(function () {
        $('#dg').datagrid({ queryParams: form2Json("searchform") });
    });
})